package com.hljx.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hljx.config.exception.DataException;
import com.hljx.dto.RolePageDTO;
import com.hljx.dto.SysRoleDTO;
import com.hljx.dto.SysRoleMenuDTO;
import com.hljx.entity.SysMenu;
import com.hljx.entity.SysRole;
import com.hljx.entity.SysRoleMenuRelation;
import com.hljx.mapper.sys.SysRoleMapper;
import com.hljx.service.SysMenuService;
import com.hljx.service.SysRoleMenuRelationService;
import com.hljx.service.SysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hljx.utils.TreeUtils;
import com.hljx.vo.RoleVO;
import com.hljx.vo.SysMenuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author gx
 * @since 2023-09-25
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    SysRoleMenuRelationService roleMenuService;

    @Autowired
    SysMenuService sysMenuService;

    @Override
    public List<RoleVO> getRoles() {
        List<SysRole> sysRoles = baseMapper.selectList(new LambdaQueryWrapper<SysRole>().eq(SysRole::getStatus,1));
        List<RoleVO> list = new ArrayList<>();
        for (SysRole sysRole : sysRoles) {
            RoleVO rolesVO = new RoleVO();
            rolesVO.setId(sysRole.getId());
            rolesVO.setRoleName(sysRole.getRoleName());
            list.add(rolesVO);
        }
        return list;
    }

    @Override
    public Boolean saveOrUpdateNew(SysRoleDTO dto) {
        Long count = baseMapper.selectCount(new LambdaQueryWrapper<SysRole>().eq(SysRole::getRoleName, dto.getRoleName())
                .ne(dto.getId() != null, SysRole::getId, dto.getId()));
        if(count>0){
            throw new DataException("角色已存在",201);
        }
        SysRole sysRole = new SysRole();
        BeanUtil.copyProperties(dto,sysRole);
        return saveOrUpdate(sysRole);
    }

    @Override
    public IPage<RoleVO> selectRoleList(RolePageDTO dto) {
        Page<RoleVO> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        return baseMapper.selectRoleList(page,dto);
    }

    @Override
    public Boolean removeSysRole(Long id) {
        return remove(new LambdaUpdateWrapper<SysRole>().set(SysRole::getDeleteFlag,1).eq(SysRole::getId,id));
    }

    @Override
    public Boolean updateRoleStatus(Long id, Integer status) {
        return update(new LambdaUpdateWrapper<SysRole>().eq(SysRole::getId,id).set(SysRole::getStatus,status));
    }

    @Override
    public Boolean addAuthMenu(SysRoleMenuDTO dto) {
        if(CollectionUtil.isEmpty(dto.getMenuIdList())){
            throw new DataException("请选择菜单",201);
        }
        return roleMenuService.saveOrUpdateNew(dto);
    }

    @Override
    public List<Long> selectMenuId(Long roleId) {
        return roleMenuService.list(new LambdaQueryWrapper<SysRoleMenuRelation>()
                .eq(SysRoleMenuRelation::getRoleId,roleId)).stream().map(SysRoleMenuRelation::getMenuId).collect(Collectors.toList());
    }

    @Override
    public List<SysMenuVO> selectSysMenuVO(Long roleId) {
        //查询该角色关联的菜单
        List<Long> ids = roleMenuService.list(new LambdaQueryWrapper<SysRoleMenuRelation>()
                         .eq(SysRoleMenuRelation::getRoleId, roleId)).stream().map(SysRoleMenuRelation::getMenuId).toList();
        List<SysMenu> sysMenus = sysMenuService.listByIds(ids);
        List<SysMenuVO> menuVos = new ArrayList<>();
        sysMenus.forEach(s->{
            SysMenuVO sysMenuVO = new SysMenuVO();
            BeanUtil.copyProperties(s,sysMenuVO);
            menuVos.add(sysMenuVO);
        });
        //生成树形菜单，子节点在children里面
        return TreeUtils.generateTrees(menuVos);
    }
}

